var mongoose = require('mongoose')
mongoose.Promise = Promise;

var db = mongoose.createConnection('localhost', 'example1_test7')

db.on('error', console.error.bind(console, '连接错误'))
db.once('open', function () {
    var PersonSchema = new mongoose.Schema({
        name:{
            first: String,
            last: String
        }
    })
    PersonSchema.virtual('name.full').get(function(){
        return this.name.first + ' ' + this.name.last
    })
    PersonSchema.virtual('name.full').set(function (name) {
        var split = name.split(' ')
        this.name.first = split[0];
        this.name.last = split[1];
    })

    var PersonModel = db.model('Person', PersonSchema)
    var personEntity = new PersonModel({
        name: {
            first: 'h',
            last: 'mc'
        }
    })
    console.log(personEntity.name.full);

    personEntity.name.full = 'h1 mc1';
    console.log(personEntity.name.first)

})